package com.education.platform.management.service.impl;

import com.education.platform.common.enums.PermissionTypeEnum;
import com.education.platform.common.enums.PriceTypeEnum;
import com.education.platform.common.exception.BusinessException;
import com.education.platform.common.service.impl.BaseServiceImpl;
import com.education.platform.common.util.BeanUtil;
import com.education.platform.common.util.StringUtils;
import com.education.platform.dao.ArticleDao;
import com.education.platform.dao.CourseDao;
import com.education.platform.dao.CoursewareDao;
import com.education.platform.dao.entity.ArticleDO;
import com.education.platform.dao.entity.CourseDO;
import com.education.platform.dao.entity.CoursewareDO;
import com.education.platform.dao.model.CourseModel;
import com.education.platform.management.config.ThreadUser;
import com.education.platform.management.service.CourseService;
import com.education.platform.management.vo.request.ArticleCourseVO;
import com.education.platform.management.vo.request.CourseUpdateVO;
import com.education.platform.management.vo.request.CoursewareUpdateVO;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * describe: 课程
 *
 * @author : tomato
 * @date: 2020-06-29 10:28:07.058
 **/
@Service
public class CourseServiceImpl extends BaseServiceImpl<CourseDao, CourseDO> implements CourseService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CourseServiceImpl.class);

    @Autowired
    private CourseDao courseDao;
    @Autowired
    private CoursewareDao coursewareDao;
    @Autowired
    private ArticleDao articleDao;


    /**
     * 保存课程及详情
     *
     * @param vo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(CourseUpdateVO vo) {
        if (vo.getId() == null) {
            this.insertCourse(vo);
        } else {
            this.updateCourse(vo);
        }
    }

    /**
     * 新增
     *
     * @param vo
     */
    private void insertCourse(CourseUpdateVO vo) {
        CourseDO courseDO = BeanUtil.convert(vo, CourseDO.class);
        //默认不推荐
        if (courseDO.getRecommend() == null) {
            courseDO.setRecommend(false);
        }
        //默认都是豆子购买
        courseDO.setPriceType(PriceTypeEnum.BEAN.getValue());
        //默认价格0
        if(courseDO.getPrice() == null){
            courseDO.setPrice(0f);
        }
        //添加当前用户为课程教师
        courseDO.setCourseTeacherId(ThreadUser.getUserId());
        courseDO.setCourseTeacherName(ThreadUser.getUserName());
        courseDO.setCreateTime(new Date());
        courseDO.setCreateBy(ThreadUser.getUserName());
        //观看次数
        courseDO.setViewsNumber(0);
        courseDO.setLikeCount(0);
        courseDO.setCollectCount(0);
        courseDO.setPublish(false);
        courseDO.setSelectedCommentCount(0);
        courseDO.setDeleted(false);
        courseDO.setSalesVolume(0);
        //保存课程详情  视频  或者  是文章
        if (vo.getFileType() == 1) {
            courseDao.insert(courseDO);
            List<CoursewareUpdateVO> videos = vo.getVideoList();
            if (CollectionUtils.isEmpty(videos)) {
                throw new BusinessException("未上传视频");
            }
            List<CoursewareDO> coursewareDOS = BeanUtil.convert(videos, CoursewareDO.class);
            //关联课程id
            coursewareDOS.parallelStream().forEach(i -> {
                i.setCourseId(courseDO.getId());
                i.setCreateBy(ThreadUser.getUserName());
                i.setName(i.getTypeName());
                i.setCreateTime(new Date());
                i.setPermissionType(PermissionTypeEnum.VIP.getValue());
            });
            coursewareDao.insertInBatch(coursewareDOS);
        } else if (vo.getFileType() == 2) {
            //课程详情为文章
            ArticleCourseVO articleCourseVO = vo.getArticle();
            if (articleCourseVO == null) {
                throw new BusinessException("文章详情不能为空");
            }
            ArticleDO articleDO = BeanUtil.convert(articleCourseVO, ArticleDO.class);

            articleDO.setOutline(articleDO.getTitle());

            if (vo.getGradeCode().startsWith("ME")) {
                articleDO.setArticleType("ME");
                articleDO.setArticleTypeName("中考冲刺");
            } else if (vo.getGradeCode().startsWith("HE")) {
                articleDO.setArticleType("HE");
                articleDO.setArticleTypeName("高考冲刺");
            }

            articleDO.setViewNumber(0);
            articleDO.setLikeCount(0);
            articleDO.setCollectCount(0);
            articleDO.setPublish(false);
            articleDO.setSelectedCommentCount(0);
            articleDO.setDeleted(false);

            articleDO.setCreateTime(new Date());
            articleDO.setCreateBy(ThreadUser.getUserName());
            articleDO.setSubType(vo.getGradeCode());
            articleDO.setSubTypeName(vo.getGradeName());

            articleDao.insert(articleDO);
            //课程关联文章id
            courseDO.setDetailId(articleDO.getId());
            courseDao.insert(courseDO);
        } else {
            throw new BusinessException("未知的课程详情类型");
        }

    }

    /**
     * 修改
     *
     * @param vo
     */
    private void updateCourse(CourseUpdateVO vo) {
        CourseDO courseDO = BeanUtil.convert(vo, CourseDO.class);
        courseDO.setUpdateBy(ThreadUser.getUserName());
        courseDO.setUpdateTime(new Date());
        courseDao.updateById(courseDO);
        if (vo.getFileType() == null) {
            return;
        }
        if (vo.getFileType() == 1) {
            List<CoursewareUpdateVO> videos = vo.getVideoList();
            if (CollectionUtils.isNotEmpty(videos)) {
                List<CoursewareDO> coursewareDOS = BeanUtil.convert(videos, CoursewareDO.class);
                for (CoursewareDO update : coursewareDOS) {
                    if (update.getId() == null) {
                        throw new BusinessException("更新视频id必传");
                    }
                    update.setPermissionType(PermissionTypeEnum.VIP.getValue());
                    update.setUpdateBy(ThreadUser.getUserName());
                    update.setUpdateTime(new Date());
                    if (StringUtils.isNotEmpty(update.getTypeName())) {
                        update.setName(update.getTypeName());
                    }
                }
                coursewareDao.updateInBatch(coursewareDOS);
            }
        } else if (vo.getFileType() == 2) {
            //课程详情为文章
            ArticleCourseVO articleCourseVO = vo.getArticle();
            if (articleCourseVO != null) {
                if (articleCourseVO.getId() == null) {
                    throw new BusinessException("更新文章id必传");
                }
                ArticleDO update = BeanUtil.convert(articleCourseVO, ArticleDO.class);
                update.setUpdateTime(new Date());
                update.setUpdateBy(ThreadUser.getUserName());
                articleDao.updateById(update);
            }

        }


    }

    /**
     * 查询课程列表
     *
     * @param query
     * @return
     */
    @Override
    public List<CourseModel> getCourseList(CourseModel query) {
        return courseDao.getCourseListBms(query);
    }
}
